package com.example.javaeespringioc.mapper;

import com.example.javaeespringioc.controller.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper {
    @Select("select id, username, password, age, gender, phone," +
            "delete_flag as deleteFlag, create_time as createTime, update_time as updateTime from userInfo " +
            "where id = #{id} and username = #{username}")
    UserInfo queryUserInfo(@Param("id") Integer i, @Param("username") String name);

    //@Select("select * from userInfo where username like '%${name}%'")
    @Select("select * from userInfo where username like concat('%', #{name}, '%')")
    List<UserInfo> queryUserInfoByName(String name);

    @Select("select * from userInfo where id = #{id}")
    @Results(id = "resultMap",value = {
            @Result(column = "delete_flag", property = "deleteFlag"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    UserInfo queryUserInfoById(Integer id);

    @ResultMap(value = "resultMap")
    @Select("select * from userInfo order by id ${sort}")
    List<UserInfo> queryAll(String sort);

    @Select("<script>" +
            "select * from userInfo " +
            "<where>" +
            "<if test='username!=null'>" +
            "username = #{username} " +
            "</if>" +
            "<if test='gender!=null'>" +
            "and gender = #{gender}" +
            "</if>" +
            "</where>" +
            "</script>")
    List<UserInfo> queryBy(@Param("username") String username, @Param("gender") Integer gender);
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into userInfo (username, password, age, gender, phone) " +
            "values (#{username}, #{password}, #{age}, #{gender}, #{phone})")
    Integer insert(UserInfo userInfo);

    @Insert("insert into userInfo (username, password, age, gender, phone) " +
            "values (#{userInfo.username}, #{userInfo.password}, #{userInfo.age}, #{userInfo.gender}, #{userInfo.phone})")
    public Integer insertByParam(@Param("userInfo") UserInfo userInfo);

    @Delete("delete from userInfo where id = #{id}")
    public Integer deleteById(Integer id);

    @Update("update userInfo set username = #{username} where id = #{id}")
    public Integer updateById(@Param("username") String username, @Param("id") Integer id);
}
